﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Commands.CottagesCommands
{
    public class CottageAllDeleteHander : IRequestHandler<CottageAllDeleteCommand, Result>
    {

        private readonly IBaseRepository<Cottages> cottageRepository;
        private readonly ILogger<CottageAllDeleteHander> logger;

        public CottageAllDeleteHander(IBaseRepository<Cottages> cottageRepository, ILogger<CottageAllDeleteHander> logger)
        {
            this.cottageRepository = cottageRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 批量删除栋舍信息
        /// </summary>
        /// <param name="request">批量删除栋舍信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>批量删除栋舍信息响应对象</returns>
        public async Task<Result> Handle(CottageAllDeleteCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                string[] strings = request.Ids.Split(',');
                foreach (var item in strings) 
                {
                    var info = cottageRepository.GetAll().FirstOrDefault(c => c.CottagesId == Convert.ToInt32(item));
                    info.Deletes = true;
                    info.DeleteTime = DateTime.Now;
                    result.Code +=  cottageRepository.Update(info);
                }
                logger.LogInformation("批量删除栋舍信息方法调用成功");
                return result;

             }
            catch (Exception ex)
            {
                logger.LogError("删除栋舍信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
